package ar.edu.itba.it.pdc.protocol;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import ar.edu.itba.it.pdc.app.client.Client;
import ar.edu.itba.it.pdc.protocol.exception.InvalidPOP3FormatException;
import ar.edu.itba.it.pdc.utils.TempFilesSetup;

public abstract class POP3Response implements IPOP3Response {
	
	public static final String PREFIX = TempFilesSetup.PREFIX_RESPONSE;
	public static final String SUFFIX = TempFilesSetup.SUFFIX;
	public static final File DIR = TempFilesSetup.DIR;
	protected static final String CRLF = TempFilesSetup.CRLF;
	
	protected File response;
	
	public ValidPOP3Response asValid(){
		return (ValidPOP3Response) this;
	}
	
	public InvalidPOP3Response asInvalid(){
		return (InvalidPOP3Response) this;
	}
	
	public File getResponse(){
		return this.response;
	}
	
	public void setResponse(File response){
		this.response = response;
	}
		
	public static File createTempFile(InputStream stream) throws IOException {
		/*File file = File.createTempFile(PREFIX, SUFFIX, DIR);
		InputStreamReader reader = new InputStreamReader(stream);
		FileWriter writer = new FileWriter(file);
		
		int c = reader.read();
		writer.write((char)c);
		while( reader.ready() && (c=reader.read()) != -1 ) {
			writer.write((char)c);
			writer.flush();				
		}
		writer.flush();
		return file;*/

		File file = File.createTempFile(PREFIX, SUFFIX, DIR);
		BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
		PrintWriter writer = new PrintWriter(new FileOutputStream(file), true);
		
		String line = reader.readLine();
		//System.out.println(line);
		writer.println(line);
		while( reader.ready() && (line = reader.readLine()) != null ) {
			//System.out.println("******" + line);
			writer.println(line);
		}
		return file;
	}
	
	public static POP3Response build(InputStream stream) throws IOException {
		POP3Response response;
		File file = createTempFile(stream);
		try {
			response = new ValidPOP3Response(file);
		} catch(InvalidPOP3FormatException e){
			response = new InvalidPOP3Response();
		}
		response.setResponse(file);
		return response;
	}

	public String getResponseString() throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(this.response)));
		String response = reader.readLine() + " ...";
		return response;
	}
	
	public long getSize(){
		return this.response.length();
	}
	
	public void delete(){
		this.response.delete();
	}

	public void receiveIn(Client client) throws IOException {
		PrintWriter writer = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(this.response)));
		String line;
		while( (line = reader.readLine()) != null )
			writer.write(line + CRLF);
		writer.flush();
	}
}
